package com.bluegalaxy.nhb.sntimer;

import com.bluegalaxy.nhb.snservice.common.exception.SNException;
import com.bluegalaxy.nhb.snservice.support.cron.SNCron;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 入口启动类
 */
@SpringBootApplication(scanBasePackages = {"com.bluegalaxy.nhb"})
@EnableTransactionManagement
@tk.mybatis.spring.annotation.MapperScan(basePackages = "com.bluegalaxy.nhb.snservice.dao")
public class CkTimerApplication {

    private static Logger logger = LoggerFactory.getLogger(CkTimerApplication.class);

    public static void main(String[] args) throws SNException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        showAnimal();
        if (args.length == 0){
            logger.info("Args error: Please start such as 'java -jar xxx.jar 定时任务注册 [超时时间]'");
            throw new SNException("Args error: Please start such as 'java -jar xxx.jar 定时任务注册 [超时时间]'");
        }else{

            logger.info("本次调用开始启动");
            String timerName = args[0];
            /**
             * 默认执行1小时
             */
            long timeout = 1000*60*60;
            if (args.length > 1){
                timeout = Long.valueOf(args[1]);
            }
            Long start = System.currentTimeMillis();
            //加载spring
            logger.info("== loading spring ==");
            SpringApplication.run(CkTimerApplication.class, args);
            logger.info("== loading spring success ==");
            //执行定时任务
            logger.info("== sn timer start ==");
            //反射生成类
            Class clazz = Class.forName("com.bluegalaxy.nhb.sntimer.timer."+timerName);
            Constructor c = clazz.getConstructor(String.class, long.class, boolean.class);
            SNCron cron = (SNCron) c.newInstance(timerName,timeout,true);
            try {
                cron.start();
                logger.info("start timer success");
            }catch (Exception e){
                logger.info("start timer error");
                logger.error(e.getMessage(),e);
            }
            logger.info("start timer end");
        }
    }

    public static void showAnimal(){
        String animal = "\n" +
                "　┏┓　　　┏┓\n" +
                "┏┛┻━━━┛┻┓\n" +
                "┃　　　　　　　┃\n" +
                "┃　　　━　　　┃\n" +
                "┃　＞　　　＜　┃\n" +
                "┃　　　　　　　┃\n" +
                "┃...　⌒　...　┃\n" +
                "┃　　　　　　　┃\n" +
                "┗━┓　　　┏━┛\n" +
                "　　┃　　　┃　Code is far away from bug with the animal protecting\n" +
                "　　┃　　　┃   神兽保佑,代码无bug\n" +
                "　　┃　　　┃\n" +
                "　　┃　　　┃\n" +
                "　　┃　　　┃\n" +
                "　　┃　　　┃\n" +
                "　　┃　　　┗━━━┓\n" +
                "　　┃　　　　　　　┣┓\n" +
                "　　┃　　　　　　　┏┛\n" +
                "　　┗┓┓┏━┳┓┏┛\n" +
                "　　　┃┫┫　┃┫┫\n" +
                "　　　┗┻┛　┗┻┛";
        logger.info(animal);
    }

}
